﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PanhuaWin.工厂.仓库.超级管理员
{
    public partial class Form修改生产情况 : UIPage
    {
        public Form修改生产情况()
        {
            InitializeComponent();
        }

        private void 修改生产情况_Load(object sender, EventArgs e)
        {
            uiDataGridView酸洗.AutoGenerateColumns = false;
            uiDataGridView镀锌.AutoGenerateColumns = false;
            uiDataGridView轧硬.AutoGenerateColumns = false;
            uiDataGridView彩涂.AutoGenerateColumns = false;
            uiDataGridView罩退.AutoGenerateColumns = false;
            uiDataGridView小标签.AutoGenerateColumns = false;
        }

        private void uiButton查询_Click(object sender, EventArgs e)
        {
            query();
        }

        private void query() 
        {
            queryPickling();
            queryZin();
            queryHard();
            queryPaint();
            queryCoverFire();
            queryLabelPrint();
        }

        //查询酸洗报工
        private void queryPickling()
        {
            string sql = @"SELECT '酸洗' 工序, OutputGuid, guid, Row 序号, OutputDate 生产日期, VolumeCodeNew 投入卷号, OutputVolumeNew 产出卷号, OutputSize 产出规格, 
                            OutputThick 产出厚度,OutputWidth 产出宽度, ProductWidth, OutputMaterial 产出材质, OutputWeight 产出重量, OutputMeter 产出长度, 
                            OutputQualityGrade 产出等级, OutputQualityInformation 质量信息, 
                            OutputEndStatus, OutputNext 去向, OutputAddress 产地, PrintTimes, DepartCode, DepartName , WorkLineCode, WorkLineName 产线名称, 
                            WorkTeamCode, WorkTeamName 班组名称, DayShiftCode, DayShiftName 班别, InventoryCode, InventoryName, FaceDone, FaceStatus, 
                            QualityStandard 执行标准, CreateID, Creator 报工人, CreateDate, ModifyID, Modifier, ModifyDate, Customer 客户
                            FROM Bus_PicklingOutput where VolumeCodeNew like '%" + uiTextBox卷号.Text.Trim() + "%'";
            DataTable dt = pub.GetDataTable(pub.ConnectionString, sql);
            uiDataGridView酸洗.DataSource = dt;
            uiDataGridView酸洗.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            uiDataGridView酸洗.Refresh();
        }

        //查询镀锌
        private void queryZin()
        {
            string sql = @"SELECT '镀锌'工序, OutputGuid, guid, VolumeCodeNew 投入卷号, Row 序号, OutputDate 生产日期, OutputVolumeNew 产出卷号, OutputSize 产出规格, 
                            OutputThick 产出厚度, ZincThick 锌层厚度, 
                            OutputWidth 产出宽度, ProductWidth, OutputMaterial 产出材质, OutputWeight 产出重量, OutputMeter 产出长度,
                            OutputAddress 产地, OutputQualityGrade 产出等级, 
                            OutputQualityInformation 质量信息, OutputEndStatus, OutputNext 去向, PrintTimes, DepartCode, DepartName, WorkLineCode, 
                            WorkLineName 产线名称, WorkTeamCode, WorkTeamName 班组名称, DayShiftCode, DayShiftName 班别, InventoryCode, InventoryName, 
                            FaceDone 表面处理, FaceStatus 表面状态, QualityStandard 执行标准, CreateID, Creator 报工人, CreateDate, ModifyID, Modifier, ModifyDate, Customer 客户
                            FROM      Bus_ZincOutput where VolumeCodeNew like '%" + uiTextBox卷号.Text.Trim() + "%'";
            DataTable dt = pub.GetDataTable(pub.ConnectionString, sql);
            uiDataGridView镀锌.DataSource = dt;
         
                 this.uiDataGridView镀锌.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
                 this.uiDataGridView镀锌.Refresh();
        }

        //轧硬
        private void queryHard()
        {
            string sql = @"SELECT  '轧硬' 工序, OutputGuid, guid, Row 序号, OutputDate 生产日期, VolumeCodeNew 投入卷号, OutputVolumeNew 产出卷号, OutputSize 产出规格,
                            OutputThick 产出厚度, 
                            OutputWidth 产出宽度, ProductWidth, OutputMaterial 产出材质, OutputWeight 产出重量, OutputMeter 产出长度, OutputAddress 产地
                            , OutputQualityGrade 产出等级, 
                            OutputQualityInformation 质量信息, OutputEndStatus, OutputNext 去向, PrintTimes, DepartCode, DepartName, WorkLineCode, 
                            WorkLineName 产线名称, WorkTeamCode, WorkTeamName 班组名称, DayShiftCode, DayShiftName 班别, InventoryCode, InventoryName, 
                            FaceDone, FaceStatus, QualityStandard 执行标准, CreateID, Creator 报工人, CreateDate, ModifyID, Modifier, ModifyDate, Customer 客户
                            FROM      Bus_HardOutput where VolumeCodeNew like '%" + uiTextBox卷号.Text.Trim() + "%'";
            DataTable dt = pub.GetDataTable(pub.ConnectionString, sql);
            uiDataGridView轧硬.DataSource = dt;
            this.uiDataGridView轧硬.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            this.uiDataGridView轧硬.Refresh();
        }

        //彩涂
        private void queryPaint()
        {
            string sql = @"SELECT '彩涂' 工序,  OutputGuid, guid, Row 序号, OutputDate 生产日期, VolumeCodeNew 投入卷号, OutputVolumeNew 产出卷号, OutputSize 产出规格, 
                            OutputThick 产出厚度, 
                            OutputWidth 产出宽度, ProductWidth, OutputMaterial 产出材质, OutputWeight 产出重量, OutputMeter 产出长度, OutputAddress 产地, OutputColor 颜色, 
                            OutputQualityGrade 产出等级, OutputQualityInformation 质量信息, OutputEndStatus, OutputNext 去向, PrintTimes, DepartCode, DepartName, 
                            WorkLineCode, WorkLineName 产线名称, WorkTeamCode, WorkTeamName 班组名称, DayShiftCode, DayShiftName 班别, InventoryCode, 
                            InventoryName, FaceDone, FaceStatus, QualityStandard 执行标准, CreateID, Creator 报工人, CreateDate, ModifyID, Modifier, ModifyDate, 
                            Customer
                            FROM      Bus_PaintOutput where VolumeCodeNew like '%" + uiTextBox卷号.Text.Trim() + "%'";
            DataTable dt = pub.GetDataTable(pub.ConnectionString, sql);
            uiDataGridView彩涂.DataSource = dt;
            this.uiDataGridView彩涂.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            this.uiDataGridView彩涂.Refresh();
        }

        //罩退
        private void queryCoverFire()
        {
            string sql = @"SELECT '罩退' 工序, OutputGuid, guid, Row 序号, OutputDate 生产日期, VolumeCodeNew 投入卷号, OutputVolumeNew 产出卷号, OutputSize 产出规格, 
                            OutputThick 产出厚度, 
                            OutputWidth 产出宽度, ProductWidth, OutputMaterial 产出材质, OutputWeight 产出重量, OutputMeter 产出长度, OutputAddress 产地,
                            OutputQualityGrade 产出等级, 
                            OutputQualityInformation 质量信息, OutputEndStatus, OutputNext 去向, PrintTimes, DepartCode, DepartName, WorkLineCode, 
                            WorkLineName 产线名称, WorkTeamCode, WorkTeamName 班组名称, DayShiftCode, DayShiftName 班别, InventoryCode, InventoryName, 
                            FaceDone 表面处理, FaceStatus 表面状态, QualityStandard 执行标准, CreateID, Creator 报工人, CreateDate, ModifyID, Modifier, ModifyDate, Customer
                            FROM      Bus_CoverFireOutput where VolumeCodeNew like '%"+ uiTextBox卷号.Text.Trim() + "%'";

            DataTable dt = pub.GetDataTable(pub.ConnectionString,sql);
            uiDataGridView罩退.DataSource = dt;
            this.uiDataGridView罩退.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            this.uiDataGridView罩退.Refresh();
        }

        //labelprint
        private void queryLabelPrint()
        { 
        string sql = @"select  guid, 来源种类, 来源产线, 来源班组, 工厂编号, 生产批号, 存货编号, 品名, 钢号, 条码, 厚度, 产地, 长度, 规格, 
                           等级, 表面状态, 表面处理, 净重, 备注, 生产日期, 检验员, 操作ip, 操作人员, 操作时间, 仓库, 货位, 锌层, 包装形式, 班组, 
                           客户, 颜色, 是否已打印, 打印次数, 是否抛送NC, 抛NC结果
                            from Bus_LablePrint where 生产批号 like '%"+ uiTextBox卷号.Text.Trim() + "%'";
            DataTable dt = pub.GetDataTable(pub.ConnectionString, sql);
            uiDataGridView小标签.DataSource = dt;
            uiDataGridView小标签.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            uiDataGridView小标签.Refresh();
        }




        private void uiDataGridView酸洗_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            //UIDataGridView view = (UIDataGridView)sender;
            string columnName = uiDataGridView酸洗.Columns[e.ColumnIndex].DataPropertyName;
            string value = uiDataGridView酸洗.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
            string guid = uiDataGridView酸洗.Rows[e.RowIndex].Cells["Cguid"].Value.ToString();
            switch (columnName)
            {
                case "生产日期":
                    edit("Bus_PicklingOutput", "OutputDate",value, guid);
                    break;
                case "产出规格":
                    edit("Bus_PicklingOutput", "OutputSize",value,guid);
                    break;
                case "产出厚度":
                    edit("Bus_PicklingOutput", "OutputThick", value,guid);
                    break;
                case "产出宽度":
                    edit("Bus_PicklingOutput", "OutputWidth", value, guid);
                    break;
                case "产出材质":
                    edit("Bus_PicklingOutput", "OutputMaterial", value, guid);
                    break;
                case "产出重量":
                    edit("Bus_PicklingOutput", "OutputWeight", value, guid);
                    break;
                case "产出长度":
                    edit("Bus_PicklingOutput", "OutputMeter", value, guid);
                    break;
                case "产出等级":
                    edit("Bus_PicklingOutput", "OutputQualityGrade", value, guid);
                    break;
                case "去向":
                    edit("Bus_PicklingOutput", "OutputNext", value, guid);
                    break;
                case "产线名称":
                    edit("Bus_PicklingOutput", "WorkLineName", value, guid);
                    break;
                case "班别":
                    edit("Bus_PicklingOutput", "DayShiftName", value, guid);
                    break;
                default:
                    break;
            }
        }

        private void uiDataGridView镀锌_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            string columnName = uiDataGridView镀锌.Columns[e.ColumnIndex].DataPropertyName;
            string value = uiDataGridView镀锌.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
            string guid = uiDataGridView镀锌.Rows[e.RowIndex].Cells["Dguid"].Value.ToString();

            //检查修改后的产出卷号有没有重复
            if (columnName == "产出卷号")
            {
                //验证没有打印过打标签
                //string sqlLabelPrint 
                //验证没有重号
                string sqlZincOutput = "select OutputVolumeNew from Bus_ZincOutput where OutputVolumeNew = '" + value + "'";
                DataTable dt = pub.GetDataTable(pub.ConnectionString, sqlZincOutput);
                if (dt != null && dt.Rows.Count > 0)
                {
                    MessageBox.Show("[" + value + "]已经存在了，请换一个");
                    return;
                }

            }
           

            switch (columnName)
            {
                case "产出卷号":
                    edit("Bus_ZincOutput", "OutputVolumeNew", value, guid);
                    break;
                case "生产日期":
                    edit("Bus_ZincOutput", "OutputDate", value, guid);
                    break;
                case "产出规格":
                    edit("Bus_ZincOutput", "OutputSize", value, guid);
                    break;
                case "产出厚度":
                    edit("Bus_ZincOutput", "OutputThick", value, guid);
                    break;
                case "产出宽度":
                    edit("Bus_ZincOutput", "OutputWidth", value, guid);
                    break;
                case "产出材质":
                    edit("Bus_ZincOutput", "OutputMaterial", value, guid);
                    break;
                case "产出重量":
                    edit("Bus_ZincOutput", "OutputWeight", value, guid);
                    break;
                case "产出长度":
                    edit("Bus_ZincOutput", "OutputMeter", value, guid);
                    break;
                case "产出等级":
                    edit("Bus_ZincOutput", "OutputQualityGrade", value, guid);
                    break;
                case "去向":
                    edit("Bus_ZincOutput", "OutputNext", value, guid);
                    break;
                case "产线名称":
                    edit("Bus_ZincOutput", "WorkLineName", value, guid);
                    break;
                case "锌层厚度":
                    edit("Bus_ZincOutput", "ZincThick", value, guid);
                    break;
                case "班别":
                    edit("Bus_PicklingOutput", "DayShiftName", value, guid);
                    break;
                default:
                    break;
            }
        }

        private void uiDataGridView轧硬_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            string columnName = uiDataGridView轧硬.Columns[e.ColumnIndex].DataPropertyName;
            string value = uiDataGridView轧硬.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
            string guid = uiDataGridView轧硬.Rows[e.RowIndex].Cells["Eguid"].Value.ToString();
            switch (columnName)
            {
                case "生产日期":
                    edit("Bus_HardOutput", "OutputDate", value, guid);
                    break;
                case "产出规格":
                    edit("Bus_HardOutput", "OutputSize", value, guid);
                    break;
                case "产出厚度":
                    edit("Bus_HardOutput", "OutputThick", value, guid);
                    break;
                case "产出宽度":
                    edit("Bus_HardOutput", "OutputWidth", value, guid);
                    break;
                case "产出材质":
                    edit("Bus_HardOutput", "OutputMaterial", value, guid);
                    break;
                case "产出重量":
                    edit("Bus_HardOutput", "OutputWeight", value, guid);
                    break;
                case "产出长度":
                    edit("Bus_HardOutput", "OutputMeter", value, guid);
                    break;
                case "产出等级":
                    edit("Bus_HardOutput", "OutputQualityGrade", value, guid);
                    break;
                case "去向":
                    edit("Bus_HardOutput", "OutputNext", value, guid);
                    break;
                case "产线名称":
                    edit("Bus_HardOutput", "WorkLineName", value, guid);
                    break;
                case "班别":
                    edit("Bus_PicklingOutput", "DayShiftName", value, guid);
                    break;
                default:
                    break;
            }
        }

        private void uiDataGridView彩涂_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            string columnName = uiDataGridView彩涂.Columns[e.ColumnIndex].DataPropertyName;
            string value = uiDataGridView彩涂.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
            string guid = uiDataGridView彩涂.Rows[e.RowIndex].Cells["Fguid"].Value.ToString();
            switch (columnName)
            {
                case "生产日期":
                    edit("Bus_PaintOutput", "OutputDate", value, guid);
                    break;
                case "产出规格":
                    edit("Bus_PaintOutput", "OutputSize", value, guid);
                    break;
                case "产出厚度":
                    edit("Bus_PaintOutput", "OutputThick", value, guid);
                    break;
                case "产出宽度":
                    edit("Bus_PaintOutput", "OutputWidth", value, guid);
                    break;
                case "产出材质":
                    edit("Bus_PaintOutput", "OutputMaterial", value, guid);
                    break;
                case "产出重量":
                    edit("Bus_PaintOutput", "OutputWeight", value, guid);
                    break;
                case "产出长度":
                    edit("Bus_PaintOutput", "OutputMeter", value, guid);
                    break;
                case "产出等级":
                    edit("Bus_PaintOutput", "OutputQualityGrade", value, guid);
                    break;
                case "去向":
                    edit("Bus_PaintOutput", "OutputNext", value, guid);
                    break;
                case "产线名称":
                    edit("Bus_PaintOutput", "WorkLineName", value, guid);
                    break;
                case "颜色":
                    edit("Bus_PaintOutput", "OutputColor", value, guid);
                    break;
                case "班别":
                    edit("Bus_PicklingOutput", "DayShiftName", value, guid);
                    break;
                default:
                    break;
            }
        }
       
        private void uiDataGridView罩退_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            string columnName = uiDataGridView罩退.Columns[e.ColumnIndex].DataPropertyName;
            string value = uiDataGridView罩退.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
            string guid = uiDataGridView罩退.Rows[e.RowIndex].Cells["Gguid"].Value.ToString();
            switch (columnName)
            {
                case "生产日期":
                    edit("Bus_CoverFireOutput", "OutputDate", value, guid);
                    break;
                case "产出规格":
                    edit("Bus_CoverFireOutput", "OutputSize", value, guid);
                    break;
                case "产出厚度":
                    edit("Bus_CoverFireOutput", "OutputThick", value, guid);
                    break;
                case "产出宽度":
                    edit("Bus_CoverFireOutput", "OutputWidth", value, guid);
                    break;
                case "产出材质":
                    edit("Bus_CoverFireOutput", "OutputMaterial", value, guid);
                    break;
                case "产出重量":
                    edit("Bus_CoverFireOutput", "OutputWeight", value, guid);
                    break;
                case "产出长度":
                    edit("Bus_CoverFireOutput", "OutputMeter", value, guid);
                    break;
                case "产出等级":
                    edit("Bus_CoverFireOutput", "OutputQualityGrade", value, guid);
                    break;
                case "去向":
                    edit("Bus_CoverFireOutput", "OutputNext", value, guid);
                    break;
                case "产线名称":
                    edit("Bus_CoverFireOutput", "WorkLineName", value, guid);
                    break;
                case "表面处理":
                    edit("Bus_CoverFireOutput", "FaceDone", value, guid);
                    break;
                case "班别":
                    edit("Bus_PicklingOutput", "DayShiftName", value, guid);
                    break;
                default:
                    break;
            }
        }

        private void edit(string 表名,string 列名,string 修改值,string guid)
        {
            if (MessageBox.Show("确定要修改么？","提示",MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                //如果是产线 要验证是不是存在的
                if (列名 == "产线名称")
                {
                    string sqlWorkLine = @"select WorkLineCode from Dic_WorkLine where WorkShopCode = '2001' and WorkLineName = '" + 修改值 + "'";
                    DataTable dt = pub.GetDataTable(pub.ConnectionString, sqlWorkLine);
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        string sqlupdate = "update " + 表名 + "set WorkLineCode = '" + dt.Rows[0][0].ToString() + "', WorkLineName = '" + 修改值 + "' where guid = '" + guid + "'";
                        int re = pub.ExecuteSql(pub.ConnectionString, sqlupdate, "修改" + 表名 + "的" + 列名 + "的值改为" + 修改值);
                        if (re < 1)
                        {
                            MessageBox.Show("修改失败");
                        }
                    }
                    else
                    {
                        MessageBox.Show("该产线名称不存在，请重新输入");
                    }

                }
                //如果是产出卷号  OutputVolumeNew，检查有没有有没有打印并推送NC了
                if (列名 == "OutputVolumeNew")
                {
                    //查询修改前的值
                    string sqlvol = "select OutputVolumeNew from Bus_ZincOutput where OutputGuid = '" + guid+"'";
                    DataTable dtvol = pub.GetDataTable(pub.ConnectionString,sqlvol);
                    if (dtvol != null && dtvol.Rows.Count > 0) 
                    {
                        //查询是否已经打印
                        string sqlLabelPrint = "select 生产批号 from Bus_LablePrint  where 生产批号 = '"
                                            + dtvol.Rows[0]["OutputVolumeNew"].ToString() + "' and 是否已打印 = '是' and 是否抛送NC = 'Y'";
                        DataTable dt = pub.GetDataTable(pub.ConnectionString, sqlLabelPrint);
                        if (dt != null && dt.Rows.Count > 0)
                        {
                            MessageBox.Show("卷号【"+ dtvol.Rows[0]["OutputVolumeNew"].ToString() + "】已经打印入库，无法修改卷号");
                            return;
                        }
                        else
                        {
                            string sqlupdateLabel = "update Bus_LablePrint set 生产批号 = '"+ 修改值 + "',条码= '"+修改值+"' where 生产批号 = '"+ dtvol.Rows[0]["OutputVolumeNew"].ToString() + "'";
                            int r1 = pub.ExecuteSql(pub.ConnectionString,sqlupdateLabel);
                            if (r1 <1)
                            {
                                MessageBox.Show("修改大标签卷号失败");
                            }
                        }

                    }
                    
                }

                string sql = "update " + 表名 + " set " + 列名 + " = '" + 修改值 + "' where OutputGuid = '" + guid + "'";
                int r = pub.ExecuteSql(pub.ConnectionString, sql, "修改" + 表名 + "的" + 列名 + "的值改为" + 修改值);
                if (r < 1)
                {
                    MessageBox.Show("修改失败");
                }
            }
            query();
        }

    }
}
